package com.oocl.customersystem.service.impl;

import java.sql.SQLException;

import com.oocl.customersystem.dao.CustomerDao;
import com.oocl.customersystem.dao.impl.CustomerDaoImpl;
import com.oocl.customersystem.exception.MyException;
import com.oocl.customersystem.pojo.Customer;
import com.oocl.customersystem.service.CustomerManager;


public class CustomerManagerImpl implements CustomerManager {
	private CustomerDao cd=new CustomerDaoImpl();

	@Override
	public int addCustomer(String cname, String pwd) throws MyException {  //注册用
		int m=0;
		if (loadCustomer(cname)==null) {
			Customer c=new Customer();
			c.setCname(cname);
			c.setPwd(pwd);
			try {
				m=cd.addCustomer(c);
			} catch (SQLException e) {
				throw new MyException("添加出错",e.getClass().getName());
			}
		}
		return m;
	}

	@Override
	public Customer loadCustomer(String cname) throws MyException {    //校验用
		Customer c=null;
		try {
			c=cd.loadCustomer(cname);
		} catch (SQLException e) {
			throw new MyException("查询出错",e.getClass().getName());
		}		
		return c;
	}

	@Override
	public boolean CustomerLogin(String cname, String pwd) throws MyException {   //登录用
		boolean flag=false;		
		Customer c=null;		
		try {
			c=cd.loadCustomer(cname);
		} catch (SQLException e) {
			throw new MyException("登录出错",e.getClass().getName());
		}
		if (pwd.equals(c.getPwd())) {
			flag=true;
		}
		return flag;
	}

}
